import pygame
from pygame.locals import *

class Camera:
    def __init__(self, screen, scale, screen_size):
        self.scale = scale
        self.camera_center = (screen_size[0] / 2, screen_size[0] / 2)
        self.camera_size = (screen_size[0] / 4, screen_size[0] / 4)
        self.screen = screen
        self.screen_size = screen_size
        self.screen_origin_size = screen_size
        self.topleft = (0, 0)
        self.camera_tag = False

    def set_screen(self, screen):
        self.screen = screen

    def camera_move(self, player_pos, direc = 1):
        player_screen_pos = self.get_screen_pos(player_pos)
        if player_screen_pos[0] > self.camera_center[0] + self.camera_size[0]\
                and direc == 1:
            self.topleft = (self.topleft[0] + player_screen_pos[0] \
                            - (self.camera_center[0] + self.camera_size[0]), 0)

        if player_screen_pos[0] < self.camera_center[0] - self.camera_size[0] \
                and direc == -1 and self.topleft[0] > 0:
            self.topleft = (self.topleft[0] + player_screen_pos[0] \
                                - (self.camera_center[0] - self.camera_size[0]), 0)

    def set_scale(self, scale):
        self.scale = scale
        self.screen_size = (self.screen_origin_size[0] * scale[0], self.screen_origin_size[1] * scale[1])
        self.camera_center = (self.screen_size[0] / 2, self.screen_size[1] / 2)
        self.camere_size = (self.screen_size[0] / 4, self.screen_size[0] / 4)

    def get_screen_pos(self, pos):
        return ((pos[0] - self.topleft[0]) * self.scale[0], \
                (pos[1] - self.topleft[1]) * self.scale[1])

    def screen_blit(self, image, image_pos):
        self.screen.blit(image, self.get_screen_pos(image_pos))

    def screen_background_blit(self, image):
        self.screen.blit(image, (0 - self.topleft[0] % self.screen_size[0], 0))
        self.screen.blit(image, (self.screen_size[0] - self.topleft[0] % self.screen_size[0], 0))
    def in_camera(self, obj_pos):
        obj_screen_pos = self.get_screen_pos(obj_pos)
        if obj_screen_pos[0] < 0 or obj_screen_pos[0] > self.screen_size[0]:
            return False
        elif obj_screen_pos[1] < 0:
            return False
        return True